package cn.aitrox.ry.service.auth.controller;

import cn.aitrox.ry.bean.CommonPageInfo;
import cn.aitrox.ry.bean.RespResult;
import cn.aitrox.ry.enumtype.RespCodeEnum;
import cn.aitrox.ry.service.auth.api.AuthRoleApi;
import cn.aitrox.ry.service.auth.dto.role.in.AuthRoleCreateInDto;
import cn.aitrox.ry.service.auth.dto.role.in.AuthRoleSearchInDto;
import cn.aitrox.ry.service.auth.dto.role.in.AuthRoleUpdateInDto;
import cn.aitrox.ry.service.auth.dto.role.out.AuthRoleDetailDto;
import cn.aitrox.ry.service.auth.service.AuthRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class AuthRoleController implements AuthRoleApi {

    @Autowired
    private AuthRoleService authRoleService;

    @PostMapping(value = "/auth/role")
    @Override
    public RespResult<Integer> create(@Validated @RequestBody AuthRoleCreateInDto input) {
        // 参数校验与处理
        if (StringUtils.isBlank(input.getName())) {
            return RespResult.secByError(RespCodeEnum.PARAM_ERROR.getCode(), "名称不能为空");
        }
        List<Integer> permissionIds = null == input.getPermissionIds() ? new ArrayList<>() : input.getPermissionIds().stream().distinct().collect(Collectors.toList());
        input.setName(input.getName().trim());
        input.setPermissionIds(permissionIds);

        return authRoleService.create(input);
    }

    @PutMapping(value = "/auth/role")
    @Override
    public RespResult update(@Validated @RequestBody AuthRoleUpdateInDto input) {

        String name = null;
        if (null != input.getName()) {
            if (StringUtils.isBlank(input.getName())) {
                return RespResult.secByError(RespCodeEnum.PARAM_ERROR.getCode(), "名称不能为空");
            }
            name = input.getName().trim();
        }


        List<Integer> roleIds = null == input.getPermissionIds() ? new ArrayList<>() : input.getPermissionIds().stream().distinct().collect(Collectors.toList());
        input.setName(name);
        input.setPermissionIds(roleIds);

        return authRoleService.update(input);
    }

    @GetMapping(value = "/auth/role/{id}")
    @Override
    public RespResult<AuthRoleDetailDto> read(@PathVariable("id") Integer id) {
        return authRoleService.read(id);
    }

    @DeleteMapping(value = "/auth/role/{id}")
    @Override
    public RespResult delete(@PathVariable("id") Integer id) {
        return authRoleService.delete(id);
    }

    @PostMapping(value = "/auth/role/search")
    @Override
    public CommonPageInfo<AuthRoleDetailDto> search(@RequestBody AuthRoleSearchInDto input) {
        return authRoleService.search(input);
    }
}
